<template>
	<view class="tab-bar">
		<view v-for="(item, index) in list" :key="index" class="tab-bar-item" :class="{
        'tab-active': selected === index}" @tap="switchTab(index)">
			<u-badge v-if="index === 3" max="99" :value="messageStats.total_count" :absolute="true"
				:offset="[5,0]"></u-badge>
			<image v-if="item.isPublish" class="tab-bar-icon" src="/static/tabbar/add.png" />
			<view class="tab-bar-text" :class="{ active: selected === index }"
				:style="{ color: selected === index ? selectedColor : color }">
				{{ item.text }}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'q-tabbar',
		props: {
			selected: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				color: '#616161',
				selectedColor: '#0fdc78',
				list: [{
						pagePath: '/pages/index/index',
						text: '发现'
					},
					{
						pagePath: '/pages/mytopic/index',
						text: '动态'
					},
					{
						pagePath: '/pages/post/add',
						text: '',
						isPublish: true
					},
					{
						pagePath: '/pages/message/message',
						text: '消息'
					},
					{
						pagePath: '/pages/my/my',
						text: '我'
					}
				],
			}
		},
		computed: {
			messageStats() {
				return this.$store.state.user.messageStats;
			}
		},
		methods: {
			switchTab(index) {
				const page = this.list[index]

				if (page.isPublish) {
					uni.navigateTo({
						url: "/pages/post/add"
					})
				} else {
					uni.switchTab({
						url: page.pagePath
					})
				}
			}
		}
	}
</script>

<style scoped>
	.tab-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 60px;
		background: #ffffff;
		display: flex;
		padding-left: 20rpx;
		padding-right: 20rpx;
		padding-bottom: env(safe-area-inset-bottom);
		z-index: 9999;
		border-radius: 20rpx 20rpx 0 0;
		box-shadow: 0 -4rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.tab-bar-item {
		position: relative;
		flex: 1;
		text-align: center;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 20rpx 0;
		border-radius: 12px;
		margin: 8px 4px;
	}

	.tab-bar-item.tab-active {
		background-color: rgba(15, 220, 120, 0.1);
	}

	.tab-bar-normal {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.tab-bar-icon {
		width: 80rpx;
		height: 80rpx;
	}

	.tab-bar-text {
		font-size: 28rpx;
		line-height: 1.2;
		font-weight: 500;
		transition: color 0.3s ease;
	}

	.tab-bar-text.active {
		color: #0fdc78 !important;
		font-weight: 600;
	}
</style>